package ar.com.truelogic.dao.generic;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

@SuppressWarnings("unchecked")
@Repository
public abstract class GenericDaoJpaImpl<T, ID extends Serializable> implements GenericDao<T, ID> {
	@PersistenceContext
	private EntityManager entityManager;
	
	Class<T> clazz = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];

	public T get(final ID id) {
		return entityManager.find(clazz, id);
	}

	public T persist(final T entity) {
		entityManager.persist(entity);
		return entity;
	}

	public void remove(final T entity) {
		entityManager.remove(entity);
	}

	public void flush() {
		entityManager.flush();
	}

	public T merge(final T entity) {
		return entityManager.merge(entity);
	}
}